package com.enlorenz.common.geo.gts.baidu;

import java.util.ArrayList;
import java.util.List;

import com.enlorenz.common.geo.gts.AbstractGtsSearcher;
import com.enlorenz.common.geo.gts.DefaultGtsFormatter;
import com.enlorenz.common.geo.gts.GtsResult;
import com.enlorenz.common.geo.gts.IGtsFormatter;
import com.enlorenz.util.DataDetUtil;
import com.enlorenz.util.StringUtil;

/**
 * 百度Gts
 * @author Karma
 */
public class BaiduGtsSearcher extends AbstractGtsSearcher {
	private IGtsFormatter gtsFormatter = new DefaultGtsFormatter();

	public static void main(String[] args) throws Exception {
		BaiduGtsSearcher s=new BaiduGtsSearcher();
		String str=s.query(112.987389, 28.195468);
		System.out.println(str);
	}
	
	@Override
	protected GtsResult queryImp(Double lon, Double lat) throws Exception {
		ResultInfoBean resultInfoBean = BaiduGtsUtil.queryGts(lon, lat);
		if(null == resultInfoBean){
			return null;
		}
		
		AddrDetail addrDetail = resultInfoBean.getResult()
				.getAddressComponent();

		GtsResult gtsResult = new GtsResult();
		gtsResult.setProvince(StringUtil.getStringIfNull(addrDetail
				.getProvince()));
		gtsResult.setCity(StringUtil.getStringIfNull(addrDetail.getCity()));
		gtsResult.setTown(StringUtil.getStringIfNull(addrDetail.getDistrict()));
		gtsResult.setStreet(StringUtil.getStringIfNull(addrDetail.getStreet())
				+ StringUtil.getStringIfNull(addrDetail.getStreet_number()));

		List<PoiInfoBean> list = resultInfoBean.getResult().getPois();
		List<String> sList = null;
		if (!DataDetUtil.isCollectionEmpty(list)) {
			sList = new ArrayList<String>();
			for (PoiInfoBean poiInfoBean : list) {
				sList.add(poiInfoBean.getName());
			}
			gtsResult.setPlaceList(sList);
		}

		return gtsResult;
	}

	public IGtsFormatter getGtsFormatter() {
		return gtsFormatter;
	}

	public void setGtsFormatter(IGtsFormatter gtsFormatter) {
		this.gtsFormatter = gtsFormatter;
	}

	@Override
	protected IGtsFormatter getFormatter(GtsResult gtsResult) {
		return gtsFormatter;
	}

}
